﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace BEELab
{
	public partial class AddHTMLControl : System.Web.UI.Page
	{
        /// <summary>
        /// Occures on every page load (first time or Post Back).
        /// Check which type of control was requested (TextBox, Radio Button, Button...),
        /// then add the appropriate control to GUI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		protected void Page_Load(object sender, EventArgs e)
		{
            //TextBox
            if (Request.QueryString["type"] != null && Request.QueryString["type"] == "text")
            {
                lblTitle.Text = "Add Textbox";
                divName.Style.Add("display", "block");
                divID.Style.Add("display", "block");
                divValue.Style.Add("display", "block");
            }
                //Redio Button / CheckBox
            else if (Request.QueryString["type"] != null && (Request.QueryString["type"] == "radio" || Request.QueryString["type"] == "checkbox"))
            {
                if (Request.QueryString["type"] == "radio")
                {
                    lblTitle.Text = "Add Radio Button";
                }
                else
                {
                    lblTitle.Text = "Add Checkbox";
                }
                divName.Style.Add("display", "block");
                divID.Style.Add("display", "block");
                divValue.Style.Add("display", "block");
                divOnClick.Style.Add("display", "block");
                divDisable.Style.Add("display", "block");
                divText.Style.Add("display", "block");
                divChecked.Style.Add("display", "block");
            }
            //Regular button
            else if (Request.QueryString["type"] != null && Request.QueryString["type"] == "button" )
            {
                lblTitle.Text = "Add Button";
                divName.Style.Add("display", "block");
                divID.Style.Add("display", "block");
                divValue.Style.Add("display", "block");
                divOnClick.Style.Add("display", "block");
                divDisable.Style.Add("display", "block");
            }
		}

        /// <summary>
        /// Generates control code-
        /// builds a string in the form of aspx  input, in order to insert in to GUI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            txtResult.Text = "<input type='" + Request.QueryString["type"] + "' ";
            
            if (txtName.Text != "")
            {
                txtResult.Text += "name='" + txtName.Text + "' ";
            }

            if (txtID.Text != "")
            {
                txtResult.Text += "ID='" + txtID.Text + "' ";
            }
            
            if (txtValue.Text != "")
            {
                txtResult.Text += "Value='" + txtValue.Text + "' ";
            }

            if (txtClick.Text != "")
            {
                txtResult.Text += "onclick='";
                if (checkDisable.Checked == true)
                {
                    txtResult.Text += "this.disabled = true; ";
                }
                txtResult.Text += txtClick.Text.Replace("'", "\"") + "' ";
            }

            if (checkChecked.Checked == true)
            {
                txtResult.Text += "checked ";
            }

            if (txtText.Text != "")
            {
                txtResult.Text += ">" + txtText.Text + "</input>";
            }
            else
            {
                txtResult.Text += "/>";
            }
        }
	}
}